<template>
	<view class="app-container">
		<view class="unreadmsg-list">
			<div class="unreadmsg-item" v-for="item in infoData" :key="item.id">
				<view class="top">
					<div>
						<h4>{{item.appInfo}}</h4>
						<u-switch v-model="item.status===1?true:false" @change="change(item.id)"></u-switch>
					</div>
					<div>
						<p>{{item.customInfo}}</p>
					</div>
				</view>
				<view class="down">
					<div>
						<text class="title">时间</text>
						<text class="value">{{item.createdAt}}</text>
					</div>
					<div>
						<text class="title">剩余余额</text>
						<text class="value">{{item.errorCode}}</text>
					</div>
				</view>
			</div>
		</view>
		<u-modal :show="show" content="您是否确认要删除当前全部消息" :showCancelButton="true" :closeOnClickOverlay="true"
			confirmColor="#00B6FC" cancelColor="#656565" @confirm="confirmDelete"></u-modal>
		<u-notify ref="uNotify" message="无消息删除"></u-notify>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getErrorsApi,
		handleError
	} from '@/api/api.js'
	export default {
		data() {
			return {
				show: false,
				value: false,
				phoneId: '',
				isSolved: '0',
				infoData: []
			}
		},
		onLoad(item) {
			this.phoneId = item.phoneId
			this.getErrors(item.phoneId)
		},
		onNavigationBarButtonTap(e) {
			let webView = this.$mp.page.$getAppWebview();
			if (e.index === 0 && this.infoData.length > 0) {
				this.show = true
			} else {
				this.$refs.uNotify.show({
					type: 'warning',
					duration: 1000,
					message: '无消息删除',
					fontSize: 14,
				})
			}
		},
		methods: {
			// 未读消息
			async getErrors(phoneId) {
				const confing = {
					phoneId: phoneId,
					isSolved: this.isSolved
				}
				const res = await getErrorsApi(confing)
				if (res.code === 10000) {
					this.infoData = res.data
				}
			},
			confirmDelete() {
				uni.showLoading({
					title: ''
				})
				const callList = this.infoData.map(item => {
					const confing = {
						phoneId: this.phoneId,
						errorId: item.id
					}
					return handleError(confing)
				})
				let results = Promise.all(callList).then(res => {
					if (res[0].code === 10000 || res.code === 10000) {
						this.getErrors(this.phoneId)
						uni.hideLoading()
						this.$refs.uToast.show({
							type: 'success',
							message: "删除成功",
						})
						this.show = false
					}

				})

			},
			// 切换按钮
			async change(id) {
				const confing = {
					phoneId: this.phoneId,
					errorId: id
				}
				const res = await handleError(confing)
				if (res.code === 10000) {
					this.$refs.uToast.show({
						type: 'success',
						message: "删除成功",
					})
					this.getErrors(this.phoneId)
				}

			}
		}
	}
</script>

<style lang="scss">
	.unreadmsg-list {
		background-color: #fff;
		padding: 12px;

		.unreadmsg-item {
			border: 1px solid #f5f6f7;
			border-radius: 10px;
			background-color: #fff;
			overflow: hidden;
			margin-bottom: 14px;

			.top {
				background-color: #f5f6f7;
				padding: 16px 12px 14px;

				>div:first-child {
					display: flex;
					justify-content: space-between;

					h4 {
						font-size: 14px;
						color: #000;
						line-height: 24px;
					}
				}

				>div:last-child {
					max-width: 70%;

					p {
						color: #656565;
						font-size: 10px;
					}
				}
			}

			.down {
				padding: 0 16px 0 16px;

				>div {
					display: flex;
					justify-content: space-between;
					line-height: 32px;
				}

				.title {
					font-size: 12px;
					color: #656565;
				}

				.value {
					color: #000;
					font-size: 12px;
				}
			}
		}
	}
</style>
